<?php

class Default_Model_TranslateModel extends RCMS_Core_BaseModel {


	protected $_configTableName = 'config';

	protected $_lexemTableName = 'lexem';

	protected $_lexemReferenceTableName = 'lexem_has_language';

	protected $_translateLanguagesTableName = 'translate_languages';

/*
	public function selectIdOfOriginalValue($originalValue) {
		$select = $this->getAdapter()->select()->from($this->_lexemTableName)->where('original_value = ?',$originalValue);
		return $this->getAdapter()->fetchOne($select);
	}

	public function insertOriginalValue($originalValue) {
		$lexemData = array('original_value'=>$originalValue);
		$this->getAdapter()->insert($this->_lexemTableName, $lexemData);
        $result = $this->getAdapter()->lastInsertId($this->_lexemTableName);
        return $result;
	}

	

	public function selectTranslatedLexem($lexemId,$languageId){
		$select = $this->getAdapter()->select()->from($this->_lexemReferenceTableName)->where('lexem_id = ?',$lexemId)->where('language_id = ?',$languageId)->limit(1);
		return $this->getAdapter()->fetchRow($select);
	}
*/

	public function selectAllConfigData() {
		$select = $this->getAdapter()->select()->from($this->_configTableName);
		return $this->getAdapter()->fetchPairs($select);
	}

	
	public function selectLexemById($lexemId) {
		$select = $this->getAdapter()->select()->from($this->_lexemTableName)->where('id = ?',$lexemId)->limit(1);
		return $this->getAdapter()->fetchRow($select, null, Zend_Db::FETCH_OBJ);
	}

	public function selectTranslatedLexem($lexemId,$languageId){
		$select = $this->getAdapter()->select()->from($this->_lexemReferenceTableName)->where('lexem_id = ?',$lexemId)->where('language_id = ?',$languageId)->limit(1);
		return $this->getAdapter()->fetchRow($select, null, Zend_Db::FETCH_OBJ);
	}

	public function insertOriginalValue($originalValue) {
		$lexemData = array('original_value'=>$originalValue);
		$this->getAdapter()->insert($this->_lexemTableName, $lexemData);
		$result = $this->getAdapter()->lastInsertId($this->_lexemTableName);
		return $result;
	}
	
	public function insertTranslatedValue($lexemId, $languageId, $translatedText) {
		$lexemData = array(
			'lexem_id'=>$lexemId,
			'language_id'=>$languageId,
			'value'=>$translatedText
		);
		
		$result = $this->getAdapter()->insert($this->_lexemReferenceTableName, $lexemData);
		return $result;
	}

	public function updateTranslatedValue($lexemId, $languageId, $translatedText) {
		$lexemData = array(
			'value'=>$translatedText
		);

		$where = ' language_id = '.intval($languageId).' AND lexem_id = '.intval($lexemId);
		$result = $this->getAdapter()->update($this->_lexemReferenceTableName, $lexemData, $where);
		return $result;
	}

	public function selectLanguageId($language){
		$select = $this->getAdapter()->select()->from($this->_translateLanguagesTableName)->where('language = ?',$language);
		return $this->getAdapter()->fetchOne($select);
	}

	public function selectLanguageById($id){
		$select = $this->getAdapter()->select()->from($this->_translateLanguagesTableName,array('language'=>'language'))->where('id = ?',$id)->limit(1);
		return $this->getAdapter()->fetchOne($select);
	}
}